package com.tgy.链表;

import java.util.Objects;

/***
 * @ClassName: DoubleLinkedListDemo
 * @Description: 双向链表测试类
 * @Auther: tanggy3
 * @Date: 14:16 2022/7/7
 * @version : V1.0
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(new DoubleLinkedNode(1,"aaa"));
        doubleLinkedList.add(new DoubleLinkedNode(2,"bbb"));
        doubleLinkedList.add(new DoubleLinkedNode(3,"ccc"));
        doubleLinkedList.add(new DoubleLinkedNode(4,"ddd"));
        doubleLinkedList.showMe();
        doubleLinkedList.add(new DoubleLinkedNode(5,"fff"));
        doubleLinkedList.showMe();
        doubleLinkedList.update(new DoubleLinkedNode(5,"ggg"));
        doubleLinkedList.showMe();
        doubleLinkedList.del(4);
        doubleLinkedList.showMe();
        System.out.println("智能增加");
        doubleLinkedList.addWithSort(new DoubleLinkedNode(4,"ggg"));
        doubleLinkedList.addWithSort(new DoubleLinkedNode(8,"ggg"));
        doubleLinkedList.addWithSort(new DoubleLinkedNode(6,"ggg"));
        doubleLinkedList.addWithSort(new DoubleLinkedNode(6,"ggg"));
        doubleLinkedList.showMe();

    }


    static void showNode(DoubleLinkedNode node){
        if (node.next == null) {
            System.out.println("链表为空");
            return;
        }
        DoubleLinkedNode temp = node;
        while (temp.next != null) {
            System.out.println(temp.next);
            temp = temp.next;
        }

    }
}


/**
 * 双向链表管理类
 */
class DoubleLinkedList {
    // 先初始化一个头节点, 头节点不要动, 不存放具体的数据
    private DoubleLinkedNode head = new DoubleLinkedNode(0, "");

    //返回头节点
    DoubleLinkedNode getHead() {
        return head;
    }

    void showMe() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        DoubleLinkedNode temp = head;
        while (temp.next != null) {
            System.out.println(temp.next);
            temp = temp.next;
        }
    }

    //添加一个节点到最后
    void add(DoubleLinkedNode inNode) {
        DoubleLinkedNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = inNode;
        inNode.pre = temp;
    }

    //添加一个节点按照顺序
    void addWithSort(DoubleLinkedNode inNode) {
        DoubleLinkedNode temp = head;
        boolean flag = false;
        while (temp.next != null) {
            if(inNode.no == temp.next.no) {
                System.out.println("节点编号重复");
                return;
            }
            if(temp.no < inNode.no && temp.next.no> inNode.no){
                //先把后一个节点保存起来
                DoubleLinkedNode nextTemp = temp.next;
                inNode.next = temp.next;
                temp.next = inNode;
                inNode.pre = temp;
                nextTemp.pre = inNode;
                flag = true;
            }
            temp = temp.next;
        }
        if(!flag){
            inNode.next = temp.next;
            temp.next = inNode;
            inNode.pre = temp;
            flag = true;
        }
    }

    //更新一个节点
    void update(DoubleLinkedNode inNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        DoubleLinkedNode temp = head;
        boolean flag = false;
        while (temp.next != null){
            if(temp.next.no == inNode.no) {
                temp.next.name = inNode.name;
                flag = true;
                System.out.println("修改完成");
                break;
            }
            temp = temp.next;
        }
        if(!flag) System.out.println("不存在此条记录，无法更新");

    }

    //删除一个节点
    void del(int no){
        //判断当前链表是否为空
        if(head.next == null){
            System.out.println("当前链表为空");
            return;
        }
        DoubleLinkedNode temp = head.next;// 辅助变量(指针)
        boolean flag = false; // 标志是否找到待删除节点的
        while (temp!=null){
            if (Objects.equals(temp.no,no)){
                flag = true;
                DoubleLinkedNode pre = temp.pre;//前一个节点
                pre.next = temp.next;//让前一个得节点直接连接到要删除节点得后一个节点
                break;
            }
            temp = temp.next;
        }

        if(flag){
            System.out.println("删除成功");
        }else {
            System.out.println("节点不存在");
        }
    }


}


// 定义DoubleLinkedNode ， 每个DoubleLinkedNode 对象就是一个节点
class DoubleLinkedNode {
    public int no;
    public String name;
    public DoubleLinkedNode next; // 指向下一个节点, 默认为null
    public DoubleLinkedNode pre; // 指向前一个节点, 默认为null
    // 构造器


    public DoubleLinkedNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    // 为了显示方法，我们重新toString
    @Override
    public String toString() {
        return "DoubleLinkedNode [no=" + no + ", name=" + name + "]";
    }
}

